/* eslint-disable no-use-before-define */

import axios, { type Response } from '@/utils/axios/axios';
import { getSHA256 } from '@/utils/axios/crypto-util';

/** 密码登录 */
export function passdLoginAxios(data: passdLoginRequest): Promise<Response<LoginResponse>> {
  data.password = getSHA256(data.password);
  return axios.post(`/system/user/login`, data) as unknown as Promise<Response<LoginResponse>>;
}

/** 刷新token */
export function refreshTokenAxios(token: string): Promise<Response<LoginResponse>> {
  return axios.post(`/system/user/refreshToken`, {}, { headers: { 'Dsbb-Custom-Access-Token': token } }) as unknown as Promise<Response<LoginResponse>>;
}

/** 修改密码 */
export function editPasswordAxios(data: EditPasswordRequest): Promise<Response<string>> {
  data.password = getSHA256(data.password);
  return axios.post(`/system/user/updatePassword`, data) as unknown as Promise<Response<string>>;
}

/** 得到用户信息 */
export function getUserInfoAxios(): Promise<Response<UserInfo>> {
  return axios.get(`/system/user/getUserInfo`) as unknown as Promise<Response<UserInfo>>;
}

/** 找回密码 */
export function findPasswordAxios(data: FindPasswordRequest): Promise<Response<string>> {
  data.password = getSHA256(data.password);
  return axios.post(`/system/user/resetPassword`, data) as unknown as Promise<Response<string>>;
}

/** 发送短信验证码 (需要手机号) */
export function sendAuthCodeByPhoneAxios(smsTypeEnum: SmsType, mobile: string): Promise<Response<string>> {
  return axios.post(`/system/mobile/sendSmsCode`, { smsTypeEnum, mobile }) as Promise<Response<string>>;
}

/** 发送短信验证码 （无需手机号） */
export function sendAuthCodeByUserAxios(smsTypeEnum: SmsType): Promise<Response<string>> {
  return axios.post(`/system/mobile/sendSmsCodeAuth`, { smsTypeEnum }) as Promise<Response<string>>;
}

/** 修改手机号 */
export function editPhoneAxios(data: EditPhoneRequest): Promise<Response<string>> {
  return axios.post(`/system/user/updatePhone`, data) as Promise<Response<string>>;
}

/** 修改自己的信息 */
export function editMyInfoAxios(data: { realname: string; avatar: string; sex: 0 | 1 | 2 }): Promise<Response<string>> {
  return axios.post(`/system/user/editInfo`, data) as Promise<Response<string>>;
}

export enum SmsType {
  /** 修改手机号(验证原手机号) */
  AppSMSChangeBindNewPhone = 'APP_SMS_CHANGE_BIND_NEW_PHONE',
  /** 修改手机号(验证新手机号) */
  AppSMSChangeBindOldPhone = 'APP_SMS_CHANGE_BIND_OLD_PHONE',
  /** 验证码登录 */
  AppSMSLogin = 'APP_SMS_LOGIN',
  /** 重置密码 */
  AppSMSRESTPassword = 'APP_SMS_REST_PASSWORD',
  /** 验证码注册 */
  AppSMSRegister = 'APP_SMS_REGISTER',
  /** 修改密码 */
  AppSMSUpdatePassword = 'APP_SMS_UPDATE_PASSWORD',
}

export type EditPhoneRequest = {
  /** 新手机号 */
  newPhone: string;
  /** 新手机号短信code */
  newSmsCode: string;
  /** 短信code */
  smsCode: string;
};

export type EditPasswordRequest = {
  /** 密码  */
  password: string;
  /** 短信code */
  smsCode: string;
};

export type FindPasswordRequest = {
  /** 密码  */
  password: string;
  /** 手机号  */
  phone: string;
  /** 短信code */
  smsCode: string;
  /** 登录账号  */
  username: string;
};

export interface passdLoginRequest {
  /** 登录密码 */
  password: string;
  /** 登录账号 */
  username: string;
}

export type LoginResponse = {
  /** 业务token  */
  accessToken: string;
  /** 刷新token  */
  refreshToken: string;
  /** 菜单权限列表 */
  permissions: string[];
  /** 角色id */
  roleId: number;
};

/**
 * 返回数据实体
 *
 * SysUserDetailAggreDTO
 */
export type UserInfo = {
  /** 管理员名称 */
  adminName: string;
  /** 头像 */
  avatar: string;
  /** 生日 yyyy-MM-dd */
  birthday: string;
  /** 创建时间 */
  createTime: string;
  /** 是否是超级管理员角色：true-是；false-否； */
  admin: boolean;
  /** 电话 */
  phone: string;
  /** 真实姓名 */
  realname: string;
  /** 性别(0-默认未知,1-男,2-女) */
  sex: 0 | 1 | 2;
  /** 状态(0-正常,1-冻结)  */
  status: number;
  /** 主键id */
  userId: number;
  /** 登录账号 */
  username: string;
  /** 角色 */
  roleName: string;
  /** 部门ID */
  deptId: number;
};
